package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.dao.third.JuheBrandMapper;
import com.zjdiepu.www.ibiz.third.IJuheBrandService;
import com.zjdiepu.www.model.third.dto.JuheBrand;
import com.zjdiepu.www.model.third.ext.JuheBrandExt;

/**
 * 品牌Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年03月23日 20时33分<br/>
 * @since 1.0<br/>
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.READ_UNCOMMITTED, timeout = 60)
public class JuheBrandServiceImpl extends BaseService implements IJuheBrandService {
	public static final Logger logger = LoggerFactory.getLogger(JuheBrandServiceImpl.class);

	@Autowired
	private JuheBrandMapper juheBrandMapper;
	
	@Override
	public String selectNameByBrandId(String thirdId) {
		JuheBrandExt juheBrandExt = new JuheBrandExt();
		juheBrandExt.setBaseWhere();
		juheBrandExt.setRows(1);
		juheBrandExt.setThirdId(thirdId);
		List<JuheBrand> juheBrands = juheBrandMapper.selectByExt(juheBrandExt);
		if(CollectionUtils.isNotEmpty(juheBrands)) {
			JuheBrand juheBrand = juheBrands.get(0);
			return juheBrand == null ? null : juheBrand.getBigPpname();
		}
		return null;
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, timeout = 60)
	public void saveUpdateAndDelete(JuheBrand juheBrand, OperateType operType) {
		switch (operType) {
		case add:
			juheBrand.initInsertData(juheBrand.getModifier(),"张三");
			juheBrandMapper.insert(juheBrand);
			break;
		case edit:
			juheBrand.initUpdateData(juheBrand.getModifier(),"张三");
			// 影响的记录数
			int updateCount = juheBrandMapper.updateByPrimaryKeySelective(juheBrand);
			if(updateCount == 0){
				throw new BaseException("CO_MSG_000012");
			} else if(updateCount > 1){
				throw new BaseException("CO_MSG_000017");
			}
			break;
		case delete:
			juheBrand.initDeleteData(juheBrand.getModifier(),"张三");
			// 影响的记录数
			int deleteCount = juheBrandMapper.updateByPrimaryKeySelective(juheBrand);
			if(deleteCount == 0){
				throw new BaseException("CO_MSG_000012");
			} else if(deleteCount > 1){
				throw new BaseException("CO_MSG_000017");
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<JuheBrand> selectByExt(JuheBrandExt juheBrandExt) {
		return juheBrandMapper.selectByExt(juheBrandExt);
	}
	
	@Override
	public int countByExt(JuheBrandExt juheBrandExt){
		return juheBrandMapper.countByExt(juheBrandExt);
	}
	
	@Override
	public JuheBrand selectById(String id) {
		return juheBrandMapper.selectByPrimaryKey(id);
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, timeout = 60)
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			juheBrandMapper.clearEmpty(fieldNames, id);
		}
	}
}
